<template>
  <div class="post-comment">
    <van-field
      class="post-field"
      v-model.trim="inputComment"
      rows="3"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="优质评论将会被优先展示"
      show-word-limit
    />
    <van-button
      type="primary"
      size="small"
      class="post-btn"
      @click="onAddComment"
      :disabled="!inputComment"
    >发布</van-button>
  </div>
</template>

<script>
import {addComment,getComments } from '@/api/comment.js'
export default {
  name: 'PostComment',
  components: {},
  props: {
      target:{
      type: [Number, String,Object],
      required: true
      },
      articleId:{
       type: [Number, String,Object]
      }
  },
  data () {
    return {
      inputComment: '',
     
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
      async onAddComment () {
          this.$toast.loading({
              message:'发布中',
              forbidClick:true//禁止背景点击

          })
  const inputComment = this.inputComment.trim()

  // 非空校验
  if (!inputComment.length) {
      this.$toast('请输入内容')
    return
  }

  // 请求添加
  const {data} = await addComment({
    target: this.target, // 评论的目标id（评论文章即为文章id，对评论进行回复则为评论id）
    content: inputComment, // 评论内容
    art_id: this.articleId? this.articleId.toString():null  // 文章id，对评论内容发表回复时，需要传递此参数，表明所属文章id。对文章进行评论，不要传此参数。
  })
  
  this.$emit('post-success',data.data.new_obj)
  // 将发布的最新评论展示到列表顶部
  //this.list.unshift(res.data.data.new_obj)

  // 清空文本框
  this.inputComment = ''
  this.$toast.success('发布成功')
}
  }
}
</script>

<style scoped lang="less">
.post-comment {
  display: flex;
  padding: 15px;
  align-items: flex-end;
  .post-field {
    background: #f5f7f9;
    margin-right: 15px;
  }
  .post-btn {
    width: 50px;
    border: none;
    padding: 0;
   
    &::before {
      display: none;
    }
  }
}
</style>